layui.use(['table', 'jquery', 'form', 'layedit'], function() {
	var table = layui.table;
	var $ = layui.jquery;
	var form = layui.form;
	var layedit = layui.layedit;
	layedit.build('content'); //建立编辑器

	table.render({
		elem: '#taskTest',
		url: 'http://localhost:8080/Online-student/TaskController/getAllTask',
		toolbar: '#taskToolbarDemo', //开启头部工具栏，并为其绑定左侧模板
		defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
			title: '提示',
			layEvent: 'LAYTABLE_TIPS',
			icon: 'layui-icon-tips'
		}],
		title: '用户数据表',
		cols: [
			[{
				type: 'checkbox',
				fixed: 'left'
			}, {
				field: 'taskid',
				title: '编号',
				width: 80,
			}, {
				field: 'cname',
				title: '班级名称',
			}, {
				field: 'coname',
				title: '课程名称',
			}, {
				field: 'taskname',
				title: '作业标题',
			}, {
				field: 'content',
				title: '作业内容',
				width: 200
			}, {
				field: 'tname',
				title: '老师姓名',
			}, {
				field: 'createtime',
				title: '创建时间',
			}, {
				fixed: 'right',
				title: '操作',
				toolbar: '#taskbarDemo',
				width: 200
			}]
		],
		page: true,
		where: {
			tid: 1
		},
		parseData: function(res) { //res 即为原始返回的数据
			return {
				"code": res.status, //解析接口状态
				"msg": res.message, //解析提示文本
				"count": res.data.total, //解析数据长度
				"data": res.data.list //解析数据列表
			};
		}
	});

	//头工具栏事件
	table.on('toolbar(taskTest)', function(obj) {
		if (obj.event == 'addTask') {
			layer.open({
				type: 1, //此处以iframe举例
				title: '教师信息管理',
				area: '850px',
				shade: 0,
				maxmin: true,
				offset: ['25%', '25%'],
				content: $("#addTaskForm"),
			})
		}
	});
	//监听行工具事件
	table.on('tool(taskTest)', function(obj) {
		var data = obj.data;
		//console.log(obj)
		if (obj.event === 'del') {
			layer.confirm('真的删除行么', function(index) {
				obj.del();
				layer.close(index);
			});
		} else if (obj.event === 'edit') {
			layer.prompt({
				formType: 2,
				value: data.email
			}, function(value, index) {
				obj.update({
					email: value
				});
				layer.close(index);
			});
		} else if (obj.event === 'lookTask') {
			layer.open({
				type: 1, //此处以iframe举例
				title: '查看作业',
				area: '850px',
				shade: 0,
				maxmin: true,
				offset: ['25%', '25%'],
				content: $("#StudentTaskTestdiv"),
				success:function(){
					initStudentTask(data.taskid);
				}
			})
		}
	});
	/**
	 * 初始化加载
	 */
	infoClassinfo();
	/**获取当前登录人所代的班级
	 * @param {Object} tid
	 */
	function infoClassinfo() {
		$.ajax({
			url: 'http://localhost:8080/Online-student/ClassinfoController/getClasssinfoBytid',
			type: 'get',
			data: {
				tid: $("#search_class").val()
			},
			dataType: "json",
			success: function(result) {
				if (result.data != null) {
					for (var i = 0; i < result.data.length; i++) {
						var opt = '<option value="' + result.data[i].cid + '">' + result.data[i]
							.cname + '</option>';
						$("#search_class").append(opt);
						$("#cid").append(opt);
					}
					form.render("select");
				}
			}
		})
	}
	/**
	 * 监听serach班级
	 */
	form.on('select(search_class)', function() {
		infoCourseinfo();
	});
	infoCourseinfo();
	/**
	 * 初始化课程下拉框
	 * @param {Object} tid
	 */
	function infoCourseinfo() {
		$.ajax({
			url: 'http://localhost:8080/Online-student/CourseController/getCourseByCid',
			type: 'get',
			async: false,
			data: {
				cid: $("#search_class").val(),
			},
			dataType: "json",
			success: function(result) {
				// console.log(result);
				if (result.data != null) {
					$("#search_course option:lt(0)").remove();
					for (var i = 0; i < result.data.length; i++) {
						var opt = '<option value="' + result.data[i].coid + '">' + result.data[i]
							.coname + '</option>';
						$("#search_course").append(opt);
					}
					form.render("select");
				}
			}
		})
	}
	//监听模糊查询
	form.on('submit(search)', function(data) {
		table.reload('taskTest', {
			where: { //设定异步数据接口的额外参数，任意设
				cid: $("#search_class").val(),
				coid: $("#search_course").val(),
				taskname: $("#search_taskname").val()
			}
		}); //只重载数据
		return false;
	});
	/**
	 * 监听发布作业的班级
	 */
	form.on('select(cid)', function() {
		infoCoInfo();
	});
	/**
	 * 初始化coid
	 */
	function infoCoInfo() {
		$.ajax({
			url: 'http://localhost:8080/Online-student/CourseController/getCourseByCid',
			type: 'get',
			async: false,
			data: {
				cid: $("#cid").val(),
			},
			dataType: "json",
			success: function(result) {
				// console.log(result);
				if (result.data != null) {
					$("#coid").html('');
					for (var i = 0; i < result.data.length; i++) {
						var opt = '<option value="' + result.data[i].coid + '">' + result.data[i]
							.coname + '</option>';
						$("#coid").append(opt);
					}
					form.render("select");
				}
			}
		})
	}
	//监听发布作业
	form.on('submit(addTaskBtn)', function(data) {
		if ($("#cid").val() == null || $("#cid").val() == '') {
			layer.msg("请选择班级");
			return false;
		}
		if ($("#coid").val() == null || $("#coid").val() == '') {
			layer.msg("请选择科目");
			return false;
		}
		if ($("#taskname").val() == null || $("#taskname").val() == '') {
			layer.msg("请输入作业标题");
			return false;
		}
		if ($("#content").val() == null || $("#content").val() == '') {
			layer.msg("请输入作业内容");
			return false;
		}
		$.ajax({
			url: 'http://localhost:8080/Online-student/TaskController/addTask',
			type: 'get',
			data: {
				cid: $("#cid").val(),
				coid: $("#coid").val(),
				taskname: $("#taskname").val(),
				content: $("#content").val(),
				tid: 1
			},
			dataType: "json",
			success: function(result) {
				// console.log(result);
				layer.closeAll();
				layer.msg(result.message);
				table.reload('taskTest');
			}
		})
		return false;
	});
});
